package com.njlime.sys.controller;

import com.njlime.core.base.controller.BaseController;
import com.njlime.core.exception.ExceptionState;
import com.njlime.core.helper.GlobalValue;
import com.njlime.core.helper.PageBean;
import com.njlime.core.web.validation.annotation.support.ValidateService;
import com.njlime.sys.entities.Unit;
import com.njlime.sys.security.util.UserUtils;
import com.njlime.sys.service.UnitService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 单位控制器，数据权限控制基础
 * Created by Dean on 2017-7-25.
 */
@Controller
@RequestMapping(value = "sys/unit")
public class UnitController extends BaseController{

    @Resource
    private UnitService unitService;


    //-------------------控制器--------------------

    /**
     * 跳转热点管理页面
     * @return
     */
    @RequestMapping(value = "unitManage")
    public ModelAndView unitManage(HttpServletRequest request) {
        ModelAndView modelAndView = new ModelAndView("/sys/terrace/unit/index");
        return modelAndView;
    }

    //------------------接口-----------------

    /**
     * 单位列表
     * @param request
     * @param unit
     * @param pageBean
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "unitList", method = RequestMethod.GET)
    public String unitList(HttpServletRequest request, Unit unit, PageBean pageBean){
        try {
            if (pageBean == null) {
                pageBean = new PageBean();
            }
            if(unit == null){
                unit = new Unit();
            }
            //return toJsonListData(unitService.list(pageBean, unit));
            return toJsonListData(unitService.unitList(getLoginUser().getRootUnit()));
        } catch (Exception e) {
            return toJsonErrorData(ExceptionState.PARAM_EXCEPTION, e.getMessage());
        }
    }

    /**
     * 单位新增
     * @param request
     * @param unit
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "add", method = RequestMethod.POST)
    public String add(HttpServletRequest request,Unit unit){
        try {
            unit.setStatus(GlobalValue.AUDITED);
            if(unit.getParent_id() == null){
                Unit root = getLoginUser().getRootUnit();
                if(root != null){
                    unit.setParent_id(root.getId());
                }else{
                    unit.setParent_id(GlobalValue.SYS_UNIT_ID);
                }
            }
            ValidateService.valid(unit);
            unit.setCreator(UserUtils.getUser().getId());
            unitService.addUnit(unit);
            return toJsonSuccessData();
        } catch (Exception e) {
            return toJsonErrorData(ExceptionState.PARAM_EXCEPTION, e.getMessage());
        }
    }

    /**
     * 获取单位信息
     * @param request
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "unitInfo", method = RequestMethod.GET)
    public String unitInfo(HttpServletRequest request,Integer id){
        try {
            return toJsonSuccessData(unitService.get(id));
        } catch (Exception e) {
            return toJsonErrorData(ExceptionState.PARAM_EXCEPTION, e.getMessage());
        }
    }

    /**
     * 编辑单位
     * @param request
     * @param unit
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "edit", method = RequestMethod.POST)
    public String edit(HttpServletRequest request,Unit unit){
        try {
            unitService.editUnit(unit);
            return toJsonSuccessData();
        } catch (Exception e) {
            return toJsonErrorData(ExceptionState.PARAM_EXCEPTION, e.getMessage());
        }
    }

    /**
     * 删除
     * @param ids
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "delete", method = RequestMethod.POST)
    public String delete(String ids){
        try {
            String[] idArray = ids.split(",");
            unitService.deleteUnit(idArray);
            return toJsonSuccessData();
        } catch (Exception e) {
            return toJsonErrorData(ExceptionState.PARAM_EXCEPTION, e.getMessage());
        }
    }

    /**
     * 获取单位数
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "unitTree",method = RequestMethod.GET)
    public String unitTree(HttpServletRequest request){
        try {
            return toJsonListData(unitService.unitTree(getLoginUser().getRootUnit(),null));
        } catch (Exception e) {
            return toJsonErrorData(ExceptionState.PARAM_EXCEPTION, e.getMessage());
        }
    }


    /**
     * 审核单位
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "audit",method = RequestMethod.POST)
    public String auditRegisterUnit(HttpServletRequest request,Unit unit){
        try {
            unitService.auditRegisterUnit(unit);
            return toJsonSuccessData();
        } catch (Exception e) {
            return toJsonErrorData(ExceptionState.PARAM_EXCEPTION, e.getMessage());
        }
    }

    @ResponseBody
    @RequestMapping(value = "checkRoot",method = RequestMethod.GET)
    public String checkRoot(HttpServletRequest request){
        try{

            Unit rootUnit = getLoginUser().getRootUnit();

            if(rootUnit == null){
                return toJsonErrorData(ExceptionState.PERMISSION_EXCEPTION,"平台用户无法操作");
            }else{
                return toJsonSuccessData();
            }
        }catch (Exception e){
            return toJsonErrorData(ExceptionState.PERMISSION_EXCEPTION,"无权进行此操作");
        }
    }

}
